package com.xuecheng.content.service;

import cn.hutool.core.io.IoUtil;
import com.xuecheng.content.config.MinioTemplate;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Map;

@Service
public class PubPageService {


    @Autowired
    private Configuration configuration;

    @Autowired
    private MinioTemplate minioTemplate;

    @Async
   public void pubPage(Map map,Long coursePubId) throws IOException, TemplateException {
        // freemarker 指定模板
        Template template = configuration.getTemplate("learing_article.ftl");
        String HTML = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);

        // 保存到MINIO
        ByteArrayInputStream stream = IoUtil.toStream(HTML.getBytes());

        minioTemplate.uploadHtml(stream,coursePubId+".html");
    }


    // 删除minio
    @Async
    public void delete(String filename){
        // 删除html 页面
        minioTemplate.removeHtml(filename);
    }


}
